﻿using LocalDbControler;
using RRQMMVVM;
using RRQMSocket;
using RRQMSocket.FileTransfer;
using System;
using System.Diagnostics;
using System.IO;
using System.Timers;

namespace ViewModel.ViewModels
{
    public class UploadFileViewModel : ViewModelBase
    {
        public UploadFileViewModel()
        {
            this.OpenFileCommand = new ExecuteCommand(OpenFile);
        }

        #region 变量

        private FileClient fileClient;
        private Timer timer;

        #endregion 变量

        #region Command

        public ExecuteCommand OpenFileCommand { get; set; }

        #endregion Command

        #region 属性

        public string FilePath { get; set; }

        private string fileName;

        public string FileName
        {
            get { return fileName; }
            set { fileName = value; OnPropertyChanged(); }
        }

        private string fileLength;

        public string FileLength
        {
            get { return fileLength; }
            set { fileLength = value; OnPropertyChanged(); }
        }

        private double uploadProgressValue;

        public double UploadProgressValue
        {
            get { return uploadProgressValue; }
            set { uploadProgressValue = value; OnPropertyChanged(); }
        }

        private string message;

        public string Message
        {
            get { return message; }
            set { message = value; OnPropertyChanged(); }
        }

        private string transferSpeed;

        public string TransferSpeed
        {
            get { return transferSpeed; }
            set { transferSpeed = value; OnPropertyChanged(); }
        }

        #endregion 属性

        #region 公共方法

        public void StartUpload(int account)
        {
            //this.fileClient = new FileClient();

            //ConnectSetting connectSetting = new ConnectSetting();
            ////connectSetting.TargetIP = AppConfig.ServiceIP;
            ////connectSetting.TargetPort = AppConfig.ServicePort;
            //try
            //{
            //    this.fileClient.Connect(connectSetting);
            //    this.fileClient.UploadFileFinshed += FileClient_UploadFileFinshed;
            //    FileUrl url = new FileUrl(this.FilePath);

            //    TransferModels.TransferMessage transferMessage = new TransferModels.TransferMessage();
            //    // transferMessage.SourceAccount = TransferClient.Selfinfo.SourceAccount;
            //    transferMessage.TargetAccount = account;
            //    url.Flag = transferMessage;
            //    this.Message = "正在上传";

            //    try
            //    {
            //        this.fileClient.UploadFile(url);
            //        timer = new Timer(1000);
            //        timer.Elapsed += Timer_Elapsed;
            //        timer.Start();
            //    }
            //    catch (Exception e)
            //    {
            //        this.Message = e.Message;
            //    }
            //}
            //catch (Exception e)
            //{
            //    this.Message = e.Message;
            //}
        }

        private void FileClient_UploadFileFinshed(object sender, FileFinishedArgs e)
        {
            this.Message = "上传完成";
            this.UploadProgressValue = 1;

            this.fileClient.Dispose();
            this.TransferSpeed = string.Empty;
            this.timer.Stop();
            this.timer.Dispose();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.UploadProgressValue = this.fileClient.UploadProgressValue;
            this.TransferSpeed = string.Format("{0}Kb/s", this.fileClient.UploadSpeed.ToString("0.00"));
        }

        #endregion 公共方法

        #region 绑定方法

        private void OpenFile()
        {
            if (File.Exists(this.FilePath))
            {
                Process.Start(this.FilePath);
            }
        }

        #endregion 绑定方法
    }
}